package com.tinyideatech.admin.controller.mall;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.tinyideatech.common.core.controller.BaseController;
import com.tinyideatech.common.core.entity.ApiResult;
import com.tinyideatech.common.core.entity.PageResult;
import com.tinyideatech.common.core.text.Convert;
import com.tinyideatech.common.utils.StringUtils;
import com.tinyideatech.system.entity.MallCart;
import com.tinyideatech.system.service.IMallCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;

/**
 * @author zhangy-5
 * @date 2019-08-27
 */

@Controller
@RequestMapping("/system/cart")
public class MallCartController extends BaseController {

    @Autowired
    private IMallCartService mallCartService;

    @GetMapping()
    public String list() {
        return "system/cart/list";
    }

    @GetMapping("/add")
    public String add() {
        return "system/cart/add";
    }

    @GetMapping("/edit/{id}")
    public String mallCartEdit(
            @PathVariable("id") Long id,
            ModelMap map
    ) {
        MallCart mallCart = mallCartService.getById(id);
        map.addAttribute("mallCart", mallCart);
        return "system/cart/edit";
    }

    @ResponseBody
    @PostMapping("/list")
    public PageResult list(MallCart mallCart) {
        QueryWrapper<MallCart> qw = new QueryWrapper<>();
        if(StringUtils.isNotEmpty("")){
            qw.like("", "");
        }
        IPage<MallCart> page = startPage();
        IPage<MallCart> mallCartList = mallCartService.page(page, qw);
        return page(mallCartList);
    }

    @ResponseBody
    @PostMapping("/save")
    public ApiResult save(
            MallCart sysRole
    ) {
        mallCartService.saveOrUpdate(sysRole);
        return success();
    }

    @ResponseBody
    @PostMapping("/remove")
    public ApiResult remove(
            String ids
    ){
        Long[] mallCartIds = Convert.toLongArray(ids);
        mallCartService.removeByIds(Arrays.asList(mallCartIds));
        return success();
    }

    @PostMapping("/changeStatus")
    @ResponseBody
    public ApiResult changeStatus(MallCart mallCart) {
        mallCartService.changeStatus(mallCart);
        return success();
    }

}
